package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.KeyTradesSearchBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 重点行业搜索
 */
public class KeyTradesSearchActivity extends BaseActivity implements IPublicBeanView {

    @BindView(R.id.tv_KeyAreaName) EditText et_KeyAreaName;
    @BindView(R.id.tv_Address) EditText et_AreaAddress;
    @BindView(R.id.sp_KeyType) Spinner sp_KeyType;
    @BindView(R.id.sp_Quality) Spinner sp_Quality;
    @BindView(R.id.tv_BusinessNum) EditText et_BusinessNum;
    @BindView(R.id.tv_Police) EditText et_Police;
    @BindView(R.id.tv_Header) EditText et_Header;
    @BindView(R.id.tv_Tel) EditText et_Telephone;
    @BindView(R.id.tv_Phone) EditText et_Phone;
    @BindView(R.id.tv_CompanyTel) EditText et_CompanyTel;
    @BindView(R.id.tv_CompanyFix) EditText et_CompanyFix;
    @BindView(R.id.et_EmployeeSmall) EditText et_EmployeeSmall;
    @BindView(R.id.et_EmployeeBig) EditText et_EmployeeBig;
    @BindView(R.id.et_MoneySmall) EditText et_MoneySmall;
    @BindView(R.id.et_MoneyBig) EditText et_MoneyBig;


    private List<GlobalBean> tradesType=new ArrayList<>();
    private List<GlobalBean> tradesQuality=new ArrayList<>();


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_key_trades_search);
        ButterKnife.bind(this);
        setTitleRight(R.string.submit);
        setTitleName("重点行业查询");
        initData();
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("行业类型");
        publicListPest.getPublicList("行业性质");
    }


    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right://提交
                submit();
                break;
        }
    }

    private void submit() {
        KeyTradesSearchBean searchBean=new KeyTradesSearchBean();
        searchBean.setKeyAreaName(et_KeyAreaName.getText().toString().trim());
        searchBean.setAddress(et_AreaAddress.getText().toString().trim());
        searchBean.setBusinessNum(et_BusinessNum.getText().toString().trim());
        searchBean.setPolice(et_Police.getText().toString().trim());
        searchBean.setHeader(et_Header.getText().toString().trim());
        searchBean.setTelephone(et_Telephone.getText().toString().trim());
        searchBean.setPhone(et_Phone.getText().toString().trim());
        searchBean.setCompanyTel(et_CompanyTel.getText().toString().trim());
        searchBean.setCompanyFix(et_CompanyFix.getText().toString().trim());
        searchBean.setEmployeeSmall(et_EmployeeSmall.getText().toString().trim());
        searchBean.setEmployeeBig(et_EmployeeBig.getText().toString().trim());
        searchBean.setMoneySmall(et_MoneySmall.getText().toString().trim());
        searchBean.setMoneyBig(et_MoneyBig.getText().toString().trim());
        String tradesType="";
        GlobalBean typeBean = (GlobalBean) sp_KeyType.getSelectedItem();
        if (typeBean!=null){
            tradesType=(typeBean.getId()).equals("-1") ?"":typeBean.getId();
        }
        searchBean.setTradesType(tradesType);
        String tradesQulity="";
        GlobalBean qualityBean = (GlobalBean) sp_Quality.getSelectedItem();
        if (qualityBean!=null){
            tradesQulity=(qualityBean.getId()).equals("-1") ?"":qualityBean.getId();
        }
        searchBean.setTradesQulity(tradesQulity);
        MyRxBus.get().post(new MsgEvent<>("KeyTradesActivity",2,searchBean));
        finish();

    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }



    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("行业性质".equals(publicBean.getDomainName())){
            tradesQuality=publicBean.getDicts();
            tradesQuality.add(0,new GlobalBean("未选择","-1"));
            if (tradesQuality!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,tradesQuality);
                sp_Quality.setAdapter(adapter);
            }
        }
        if ("行业类型".equals(publicBean.getDomainName())){
            tradesType=publicBean.getDicts();
            tradesType.add(0,new GlobalBean("未选择","-1"));
            if (tradesType!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,tradesType);
                sp_KeyType.setAdapter(adapter);

            }
        }

    }

}
